package thread;

/**
 * Created by User on 2018/1/10.
 */
public class ThreadStop {

    public static void main(String[] args) throws InterruptedException {

        RunningThread runningThread = new RunningThread();
        runningThread.start();
        VolatileRunningThread volatileRunningThread = new VolatileRunningThread();
        volatileRunningThread.start();


        Thread.sleep(1000);

        runningThread.isRunning = false;
        volatileRunningThread.isRunning = false;


    }

    static class RunningThread extends Thread {

        public Boolean isRunning;

        @Override
        public void run() {
            isRunning = true;

            while (isRunning) {
                ;
            }

            System.out.println("RunningThread退出循环 isRunning=" + isRunning);
        }
    }

    static class VolatileRunningThread extends Thread {

        public volatile Boolean isRunning;

        @Override
        public void run() {
            isRunning = true;

            while (isRunning) {
                ;
            }

            System.out.println("VolatileRunningThread退出循环 isRunning=" + isRunning);
        }
    }

}
